package LeetcodeTest_sixteen;
//猜数游戏
public class getHint
{
    public static void main(String[] args)
    {
        String s1="1122";
        String s2="1222";
        System.out.println(Solution.getHint(s1,s2));
    }
}
class Solution
{
    public static String getHint(String secret, String guess)
    {
        int i,count1,count2,a,b;
        i=count1=count2=0;
        if(secret.length()==0)
            return count1+"A"+count2+"B";
        int flag[]=new int[9];
        for(int j=0;j<secret.length(); j++)
        {
            a = secret.charAt(j) - '0' + 0;
            flag[a]++;
        }
        while(i<secret.length())
        {
            if(containChar(secret,guess.charAt(i)))
            {
                a=secret.charAt(i)-'0'+0;
                if(secret.charAt(i)==guess.charAt(i) && flag[a]>0)
                {
                    count1++;
                    flag[a]--;
                }
            }
            i++;
        }
        i=0;
        while(i<secret.length())
        {
            if(containChar(secret,guess.charAt(i)))
            {
                b=guess.charAt(i)-'0'+0;
                if(secret.charAt(i)!=guess.charAt(i) && flag[b]>0)
                {
                    count2++;
                    flag[b]--;
                }
            }
            i++;
        }
        return count1+"A"+count2+"B";
    }
    public static boolean containChar(String s,char ch)
    {
        for(int j=0;j<s.length();j++)
            if(s.charAt(j)==ch)
                return true;
        return false;
    }
}